// pages/login/login.js
const _function = require("../../utils/functionData.js")

Page({
  data: {
    roleIndex: 0,      // 身份索引
    roleText: '学生登录', // 当前显示身份
    phone: '',          // 手机号
    password: '',       // 密码
    isFormValid: false  // 表单是否有效
  },

  // 切换身份
  changeRole(e) {
    const index = e.detail.value
    this.setData({
      roleIndex: index,
      roleText: ['学生登录', '教师登录'][index]
    })
  },

  // 输入手机号
  inputPhone(e) {
    this.setData({ phone: e.detail.value })
    this.checkFormValid()
  },

  // 输入密码
  inputPassword(e) {
    this.setData({ password: e.detail.value })
    this.checkFormValid()
  },

  // 验证表单
  checkFormValid() {
    // const valid = this.data.phone.length === 11 &&
    //   this.data.password.length >= 6
    // this.setData({ isFormValid: valid })
    this.setData({ isFormValid: true })
  },
  // 处理登录
  handleLogin() {
    console.log(213);
    if (!this.data.isFormValid) return

    const loginData = {
      type: this.data.roleIndex === 0 ? '0' : '1',
      username: this.data.phone,
      password: this.data.password
    }
    
    if(this.data.roleIndex == 1){
      getApp().globalData.tarList=[
        {
          "pagePath": "/pages/index/index",
          "text": "首页",
          "iconPath": "/images/tabs/首页.png",
          "selectedIconPath": "/images/tabs/首页 (1).png"
        },
        {
          "pagePath": "/pages/message/message",
          "text": "消息",
          "iconPath": "/images/tabs/消息.png",
          "selectedIconPath": "/images/tabs/消息 (1).png"
        },
        {
          "pagePath": "/pages/appointconfirmation/appointconfirmation",
          "text": "预约确认",
          "iconPath": "/images/tabs/预约.png",
          "selectedIconPath": "/images/tabs/预约 (1).png"
        },
        {
          "pagePath": "/pages/publishvideo/publishvideo",
          "text": "发布学习视频",
          "iconPath": "/images/tabs/发布.png",
          "selectedIconPath": "/images/tabs/发布 (1).png"
        },
        {
          "pagePath": "/pages/individual/individual",
          "text": "个人中心",
          "iconPath": "/images/tabs/我的.png",
          "selectedIconPath": "/images/tabs/我的 (1).png"
        }
      ]
    }else {
      getApp().globalData.tarList=[
        {
          "pagePath": "/pages/index/index",
          "text": "首页",
          "iconPath": "/images/tabs/首页.png",
          "selectedIconPath": "/images/tabs/首页 (1).png"
        },
        {
          "pagePath": "/pages/message/message",
          "text": "消息",
          "iconPath": "/images/tabs/消息.png",
          "selectedIconPath": "/images/tabs/消息 (1).png"
        },
        {
          "pagePath": "/pages/studyplanning/studyplanning",
          "text": "学习规划",
          "iconPath": "/images/tabs/学习.png",
          "selectedIconPath": "/images/tabs/学习 (1).png"
        },
        {
          "pagePath": "/pages/Interestplanning/Interestplanning",
          "text": "兴趣规划",
          "iconPath": "/images/tabs/社团.png",
          "selectedIconPath": "/images/tabs/社团 (1).png"
        },
        {
          "pagePath": "/pages/individual/individual",
          "text": "个人中心",
          "iconPath": "/images/tabs/我的.png",
          "selectedIconPath": "/images/tabs/我的 (1).png"
        }
      ]
     
    }
    console.log('loginList',getApp().globalData.tarList);
    getApp().globalData.roleIndex = loginData.type
    _function.postwxappLogin(loginData, this.postwxappLoginBack, this)
  },
  postwxappLoginBack(res) {
    let dataAll = res.data
    if (dataAll.code == 200) {
      wx.removeStorageSync('token')
      wx.setStorageSync('token', dataAll.data.token)
      getApp().globalData.token = 'Bearer ' + wx.getStorageSync('token')
      wx.removeStorageSync('userInfo')
   
      wx.setStorageSync('userInfo', {
        userId: dataAll.data.userId,
        role: this.data.roleIndex,
        deptId: dataAll.data.deptId,
        loginTime: dataAll.data.loginTime,
        expireTime: dataAll.data.expireTime,
        ipaddr: dataAll.data.ipaddr,
        loginLocation: dataAll.data.loginLocation,
        browser: dataAll.data.browser,
        os: dataAll.data.os,
        permissions: dataAll.data.permissions,
        user: dataAll.data.user,
        username: dataAll.data.username,
        password: dataAll.data.password,
        sex: dataAll.data.sex,
        name: dataAll.data.name,
        avatar: dataAll.data.avatar,
        adddataAlls: dataAll.data.adddataAlls,
        jobTitle: dataAll.data.jobTitle,
        studentId: dataAll.data.studentId,
        professionType: dataAll.data.professionType,
        professionCategory: dataAll.data.professionCategory,
        enabled: dataAll.data.enabled,
        authorities: dataAll.data.authorities,
        accountNonExpired: dataAll.data.accountNonExpired,
        accountNonLocked: dataAll.data.accountNonLocked,
        credentialsNonExpired: dataAll.data.credentialsNonExpired
      })
      getApp().globalData.userInfo = wx.getStorageSync('userInfo');
      wx.switchTab({
        url: '/pages/index/index',
        success: (dataAll) => { },
        fail: (dataAll) => { },
        complete: (dataAll) => { },
      })
    }
    wx.showToast({
      title: dataAll.msg,
      icon: 'none', // 不显示图标
      duration: 1500, // 提示的延迟时间，单位毫秒，默认为 1500
      mask: true, // 是否显示透明蒙层，防止触摸穿透，默认为 `false`
      success: () => {
        console.log('登录失败提示已显示');
      }
    });
  },
  toRegister(){
    wx.navigateTo({
      url: '/pages/retsiger/index',
    })
  }

})